package co.gov.medellin.sdep.sisdep.security.sap.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import co.gov.medellin.sdep.sisdep.common.AppConstants;

/**
 * 
 * @author 71763606
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 * SERVER LOG: 
 * SISDEPWebSecurityFilter::portalUserID: 71763606
 * URL inicio:
 * http://epdev.medellin.gov.co:50000/espaciopublico/  
 */ 
public  final class SISDEPWebSecurityFilter implements Filter {
	
  //private FilterConfig filterConfig = null;
  
  public void init(FilterConfig filterConfig) throws ServletException {
    //this.filterConfig = filterConfig;
  }

  public void doFilter(ServletRequest request, ServletResponse response,  FilterChain chain) 
              throws IOException, ServletException {
	  	
	  	/*
		IUser user = UMFactory.getAuthenticator().getLoggedInUser(
		             (HttpServletRequest)request, (HttpServletResponse)response);
		if (null == user) {
			UMFactory.getAuthenticator().forceLoggedInUser(
			         (HttpServletRequest)request, (HttpServletResponse)response);
			return; 
		} else {
			try {
				String portalUserID =  user.getUniqueName().toUpperCase();
				int indexS = portalUserID.indexOf("S"); // Prefijo "S" adicionado por el Portal
				if(indexS != -1) {
					portalUserID = portalUserID.substring(indexS+1);
				}
				// Se registra en la session WEB el ID del usuario autenticado en el Portal SAP:
                ((HttpServletRequest)request).getSession().setAttribute("SISDEPConstants.USER_ID_PORTAL_SAP", portalUserID);

			}catch(Exception e) {
				System.err.println("SISDEPWebSecurityFilter::OCURRIO UN ERROR: " + e.getLocalizedMessage());
				e.printStackTrace(System.err);
			}
			
		}
		*/
		// Se registra en la session WEB el ID del usuario autenticado en el Portal SAP:
      ((HttpServletRequest)request).getSession().setAttribute(AppConstants.USER_ID_PORTAL_SAP, "71763606");
        chain.doFilter(request, response);
    }
  
    public void destroy() {
      //this.filterConfig = null;
    }
}
